package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.repository.condition.MassageRefCondition;
import com.spa.infrastructure.repository.mapper.MassageRefMapper;
import com.spa.infrastructure.repository.po.MassageRefPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MassageRefRepositoryService extends ServiceImpl<MassageRefMapper, MassageRefPO> {

    @Autowired
    private MassageRefMapper massageRefMapper;

    public void addMassageRef(MassageRefPO massageRefPO){
        massageRefMapper.insert(massageRefPO);
    }

    public void delMassageRef(MassageRefCondition condition) {
        LambdaUpdateWrapper<MassageRefPO> wrapper = updateWrapper(condition);
        massageRefMapper.delete(wrapper);
    }

    public List<MassageRefPO> listMassageRefByCondition(MassageRefCondition condition) {
        LambdaQueryWrapper<MassageRefPO> wrapper = wrapper(condition);
        return list(wrapper);
    }

    private LambdaQueryWrapper<MassageRefPO> wrapper(MassageRefCondition condition) {
        LambdaQueryWrapper<MassageRefPO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollectionUtil.isNotEmpty(condition.getMassagistIds()), MassageRefPO::getMassagistId, condition.getMassagistIds());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getServiceIds()), MassageRefPO::getServiceId, condition.getServiceIds());
        wrapper.eq(ObjectUtil.isNotNull(condition.getMassagistId()), MassageRefPO::getMassagistId, condition.getMassagistId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getServiceId()), MassageRefPO::getServiceId, condition.getServiceId());
        return wrapper;
    }
    public List<MassageRefPO> refList(MassageRefCondition condition){
        LambdaQueryWrapper<MassageRefPO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CollUtil.isNotEmpty(condition.getMassagistIds()),MassageRefPO::getMassagistId,condition.getMassagistIds());
        lqw.eq(ObjectUtils.isNotEmpty(condition.getMassagistId()),MassageRefPO::getMassagistId,condition.getMassagistId());

        lqw.eq(CollUtil.isNotEmpty(condition.getServiceIds()),MassageRefPO::getServiceId,condition.getServiceIds());
        lqw.eq(ObjectUtils.isNotEmpty(condition.getServiceId()),MassageRefPO::getServiceId,condition.getServiceId());
        return list(lqw);

    }


    private LambdaUpdateWrapper<MassageRefPO> updateWrapper(MassageRefCondition condition) {
        LambdaUpdateWrapper<MassageRefPO> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ObjectUtil.isNotNull(condition.getMassagistId()), MassageRefPO::getMassagistId, condition.getMassagistId());
        return wrapper;
    }
}
